package util;

import java.util.*;

public class Date implements Comparable<Date> {
	private GregorianCalendar actualDate;
	
	public Date(int day, int month, int year) {
		actualDate = new GregorianCalendar(year, month, day);
	}
	
	public Date(int day, int month, int year, int hour, int minute, int second) {
		actualDate = new GregorianCalendar(year, month, day, hour, minute, second);
	}
	
	public Date(Date d) {
		actualDate = new GregorianCalendar(d.getYear(), d.getMonth(), d.getDay(), d.getHour(), d.getMinute(), d.getSecond());
	}
	
	public int getDay() {
		return actualDate.get(GregorianCalendar.DAY_OF_MONTH);
	}
	public int getMonth() {
		return actualDate.get(GregorianCalendar.MONTH);
	}
	public int getYear() {
		return actualDate.get(GregorianCalendar.YEAR);
	}
	public int getHour() {
		return actualDate.get(GregorianCalendar.HOUR);
	}
	public int getMinute() {
		return actualDate.get(GregorianCalendar.MINUTE);
	}
	public int getSecond() {
		return actualDate.get(GregorianCalendar.SECOND);
	}
	
	public boolean before(Date d) {
		return this.actualDate.before(d.actualDate);
	}
	
	public boolean after(Date d) {
		return this.actualDate.after(d.actualDate);
	}

	public Date nextDay() {
		Date d = new Date(this);
		d.actualDate.add(GregorianCalendar.DAY_OF_MONTH, 1);
		return d;
	}

	public String toString() {
		return getMonth() + "/" + getDay() + "/" + getYear();
	}
	
	public int compareTo(Date d) {
		return actualDate.compareTo(d.actualDate);
	}
	
	public int distanceInDays(Date d) {
		Date start, end;
		if(this.before(d)) {
			start = new Date(this);
			end = new Date(d);
		}
		else {
			start = new Date(d);
			end = new Date(this);
		}
		int counter = 0;
		while(start.before(end)) {
			start = start.nextDay();
			counter++;
		}
		return counter;
	}
	
	public Date add(int numDays) {
		Date result = new Date(this);
		for(int i = 0; i < numDays; i++) {
			result = result.nextDay();
		}
		return result;
	}
	
	public boolean equals(Object o) {
		Date d = (Date) o;
		return d.getDay() == this.getDay() && d.getYear() == this.getYear() && d.getMonth() == this.getMonth();
	}
	
	public static Date getRandomDate(Date start, Date end) {
		int rand = (int) (start.distanceInDays(end) * Math.random());
		Date result = new Date(start);
		for(int i = 0; i < rand; i++) {
			result = result.nextDay();
		}
		return result;
	}

	
}
